<div class="nsy_form">
    <?php
   Util::tsRegisterAssetJs('_form.js');

    /** @var FormController $this */
    /** @var Form $model */
    /** @var AweActiveForm $form */
    Yii::app()->clientScript->registerCoreScript('jquery.ui');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/knockout-3.0.0.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/bootstrap-editable.min.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/knockout.x-editable.min.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/knockout-sortable.min.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/knockout.validation.min.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/jquery.autosize.min.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/jquery.validate.min.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/jquery.fancybox-1.3.4.js');
    Yii::app()->clientScript->registerCssFile($this->module->assetsUrl . '/css/bootstrap-editable.css');
    Yii::app()->clientScript->registerCssFile($this->module->assetsUrl . '/css/jquery.fancybox-1.3.4.css');

    // Import models and viewModel
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/models/option.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/models/field.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/models/section.js');
    Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/js/viewModels/nosey.js');

    Yii::app()->clientScript->registerScript('form_form', "$.fn.editable.defaults.mode = 'inline';
            ko.applyBindings(new NoseyViewModel());
            $('textarea').autosize();
            $('#form-form').validate();
            $('a#show-preloaded-fields').fancybox({'hideOnOverlayClick':false, 'showCloseButton':false});
            ocultarRating();
            validacionCampoMaximoRating();");
     
    $form = $this->beginWidget('ext.AweCrud.components.AweActiveForm', array(
        'id' => 'form-form',
        'enableAjaxValidation' => true,
        'clientOptions' => array('validateOnSubmit' => true, 'validateOnChange' => false,),
        'enableClientValidation' => false,
    ));
    ?>

    <p class="note">
        <?php echo Yii::t('noseyModule.app', 'Fields with') ?> <span class="required">*</span>
        <?php echo Yii::t('noseyModule.app', 'are required') ?>.
    </p>

    <?php //echo $form->errorSummary($model)  ?>


    <div class="nsy_form_row">
        <?php echo $form->textFieldRow($model, 'name', array('maxlength' => 254)) ?>
    </div>
    <div class="nsy_form_row">
        <label class="control-label">Mensaje de Bienvenida</label>
        <?php
        $this->widget(
                'bootstrap.widgets.TbToggleButton', array(
            'model' => $model,
            'attribute' => 'welcome',
            'onChange' => 'js:function($el, status, e){ValidacionToggle("Form_welcome_msg",status);}'
                )
        );
        ?>
        <div class="space5 "></div>
        <?php echo $form->textField($model, 'welcome_msg', array('maxlength' => 254, 'style' => $model->welcome==0?'display:none':'')) ?>
    </div>
    <div class="nsy_form_row">
        <label class="control-label">Mensaje de Despedida</label>
        <?php
        $this->widget(
                'bootstrap.widgets.TbToggleButton', array(
//            'name' => 'leave',
            'model' => $model,
            'attribute' => 'leave',
            'onChange' => 'js:function($el, status, e){ValidacionToggle("Form_leave_msg",status);}'
                )
        );
        ?>
        <div class="space5"></div>
        <?php echo $form->textField($model, 'leave_msg', array('maxlength' => 254, 'style' => $model->leave==0?'display:none':'')) ?>
    </div>

    <div class="nsy_field_editor">
        <h3><?php echo Yii::t('noseyModule.app', 'Form Fields') ?></h3>

        <div class="nsy_sections_container" data-bind="sortable: {data: sections, allowDrop: false, connectClass:'nsy_sections_container', afterMove:updateSectionPosition, options:{handle:'.nsy_section_header',axis:'y'}}">
            <div class="nsy_section">
                <div class="nsy_section_header">
                    <span data-bind="editable: name"></span>
                    <a class="close" data-bind="click: $root.removeSection">&times;</a>
                </div>

                <div class="btn-group">
                    <a class="btn btn-mini dropdown-toggle nsy_add_field" data-toggle="dropdown" href="#">
                        <?php echo Yii::t('noseyModule.app', 'Add Field') ?> <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a data-bind="click: addField.bind($data, '<?php echo Field::FIELD_TYPE_TEXT ?>')"><?php echo Yii::t('noseyModule.app', 'Text') ?></a></li>
                        <li><a data-bind="click: addField.bind($data, '<?php echo Field::FIELD_TYPE_SIMPLE ?>')"><?php echo Yii::t('noseyModule.app', 'Simple Choice') ?></a></li>
                        <li><a data-bind="click: addField.bind($data, '<?php echo Field::FIELD_TYPE_MULTIPLE ?>')"><?php echo Yii::t('noseyModule.app', 'Multiple Choice') ?></a></li>
                        <li><a data-bind="click: addField.bind($data, '<?php echo Field::FIELD_TYPE_BOOLEAN ?>')"><?php echo Yii::t('noseyModule.app', 'Boolean') ?></a></li>
                        <li><a data-bind="click: addField.bind($data, '<?php echo Field::FIELD_TYPE_DATE ?>')"><?php echo Yii::t('noseyModule.app', 'Date') ?></a></li>
                        <li><a data-bind="click: addField.bind($data, '<?php echo Field::FIELD_TYPE_RATING ?>')"><?php echo Yii::t('noseyModule.app', 'Rating') ?></a></li>
                        <!-- ko if: $root.createdFields().length!=0 -->
                        <li class="divider"></li>
                        <li><a data-bind="click: $root.selectSection"><?php echo Yii::t('noseyModule.app', 'Load Field') ?></a></li>
                        <!-- /ko -->
                    </ul>
                </div>

                <div class="nsy_field_container" data-bind="sortable: {data: fields, connectClass:'nsy_questions_container', afterMove:updateFieldPosition}">
                    <div class="nsy_field">
                        <div>
                            <label class="nsy_label"><?php echo Yii::t('noseyModule.app', 'Label') ?></label>
                            <textarea type="text" class="nsy_field_label required" data-bind="value: label, uniqueName: true"></textarea>
                            <!-- ko if: availableValidations().length > 0 -->
                            <a class="btn btn-mini" data-bind="click: toggleValidations"><i class="icon-ok-circle"></i><?php echo Yii::t('noseyModule.app', 'Validations') ?></a>
                            <!-- /ko -->
                            <span class="nsy_field_type">
                                (<?php echo Yii::t('noseyModule.app', 'Field Type') ?>:
                                <span class="bold" data-bind="text: formated_type"></span>
                                <!-- ko if: availableWidgets().length > 0 -->
                                , <?php echo Yii::t('noseyModule.app', 'Widget Type') ?>:
                                <span data-bind="editable: widget_type, editableOptions: {type: 'select', mode: 'popup', options: availableWidgets, optionsText: 'text', optionsValue: 'value'}"></span>
                                <!-- /ko -->
                                )
                            </span>

                            <a class="close" data-bind="click: $parent.removeField">&times;</a>
                            <a class="btn btn-mini" data-bind="click: toggleDescription"><i class="icon-info-sign"></i><?php echo Yii::t('noseyModule.app', 'Description') ?></a>
                            <div class="line" data-bind="visible: showDescription">
                                <label class="nsy_label"><?php echo Yii::t('noseyModule.app', 'Description') ?></label>
                                <textarea class="nsy_field_textarea required" data-bind="value: description, uniqueName: true"></textarea>
                            </div>

                            <!-- ko if: allowOptions -->
                            <ol class="nsy_options_container" data-bind="foreach: fieldOptions()">
                                <li class="nsy_option">
                                    <input type="text" class="required" data-bind="value: label, uniqueName: true" />
                                    <a class="close" data-bind="click: $parent.removeOption">&times;</a>
                                </li>
                            </ol>

                            <a class="btn btn-success btn-mini nsy_add_option" data-bind="click: addOption"><?php echo Yii::t('noseyModule.app', 'Add Option') ?></a>
                            <!-- /ko -->

                            <div class="nsy_validations" data-bind="visible: showValidations">
                                <label><?php echo Yii::t('noseyModule.app', 'Validations') ?></label>
                                <ul data-bind="foreach: availableValidations()">
                                    <li>
                                        <label>
                                            <input type="checkbox" data-bind="attr:{value: value}, checked: $parent.validations" />
                                            <span data-bind="text: text"></span>
                                        </label>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="nsy_empty_form" data-bind="visible: sections().length==0">
            <?php echo Yii::t('noseyModule.app', 'Empty Form') ?>
        </div>

        <a data-bind="click: addSection" class="btn btn-small btn-warning pull-right"><i class="icon-plus"></i> <?php echo Yii::t('noseyModule.app', 'Add Section') ?></a>
    </div>


    <div class="nsy_buttons">
        <?php echo $form->hiddenField($model, 'form_structure', array('data-bind' => 'value: ko.toJSON($data)')) ?>
        <?php echo $form->hiddenField($model, 'ko_data') ?>
        <?php echo CHtml::hiddenField('created_fields', $fields) ?>

        <?php
        $this->widget('bootstrap.widgets.TbButton', array(
            'buttonType' => 'submit',
            'type' => 'primary',
            'label' => $model->isNewRecord ? Yii::t('noseyModule.app', 'Create') : Yii::t('noseyModule.app', 'Save'),
        ));
        ?>
    </div>

    <?php $this->endWidget(); ?>
</div>

<a id="show-preloaded-fields" href="#preloaded-fields" style="display: none;"></a>
<div style="display:none">
    <div id="preloaded-fields" class="nsy_preloaded_fields">
        <ul data-bind="foreach: createdFields">
            <li data-bind="click: $root.addPreloadedField"><label data-bind="text: label"></label> (<?php echo Yii::t('noseyModule.app', 'Field Type') ?>:<span data-bind="text: formated_type"></span>)</li>
        </ul>
        <br>
        <a class="btn btn-small pull-right" onclick="$.fancybox.close()"><?php echo Yii::t('noseyModule.app', 'Cancel') ?></a>
    </div>
</div>
<!--pre data-bind="text: ko.toJSON($data,null,2)"></pre-->